package com.baizhi.cmfzmanager.realm;

import com.baizhi.cmfzmanager.dao.AdminDao;
import com.baizhi.cmfzmanager.entity.Admin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthenticatingRealm {

    @Autowired
    private AdminDao adminDao;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username= (String) authenticationToken.getPrincipal();
        Admin admin = adminDao.selectOne(new QueryWrapper<Admin>().eq("username", username));
        if(admin!=null){
            SimpleAccount simpleAccount=
                    new SimpleAccount(admin.getUsername(),admin.getPassword(), ByteSource.Util.bytes(admin.getSalt()),this.getName());
            return simpleAccount;

        }

        return null;
    }
}
